﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.Consts;

namespace BySoft.Server.DataAccess
{
    public partial class UserLoginDA
    {
        public static int UpdateLoginDate(string userId,DataAccessBroker broker)
        { 
                #region SQL Server
                DataAccessParameterCollection parameters = new DataAccessParameterCollection();

                string sSql = " UPDATE SYS_User SET  ";
                sSql += " LastestLoginDate = GetDate()";
                sSql += " WHERE UserID =@userId ";
                parameters.AddWithValue("@userId", userId);

                return broker.ExecuteSQL(sSql, parameters);
                #endregion
        }

        public static int Logout(string userId)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                #region SQL Server
                DataAccessParameterCollection parameters = new DataAccessParameterCollection();

                string sSql = " UPDATE " + SysLoginlogTable.C_TABLENAME + " SET  ";
                sSql += SysLoginlogTable.C_LOGOUTTIME + " = Getdate()";
                sSql += " WHERE " + SysLoginlogTable.C_USERID + " =@userId ";
                sSql += " AND " + SysLoginlogTable.C_LOGINTIME + " = ";
                sSql += " (SELECT MAX(" + SysLoginlogTable.C_LOGINTIME + ") FROM " + SysLoginlogTable.C_TABLENAME + " WHERE " + SysLoginlogTable.C_USERID + " =@userId )";
                parameters.AddWithValue("@userId", userId);

                return broker.ExecuteSQL(sSql, parameters);
                #endregion
            }
        }


        public static DataTable GetLoginLogList(string loginUser, string startDate,
                string endDate, string ip, string loginResult, int pageIndex, int pageSize, out int count)
        {
                #region SQL Server
                DataAccessParameterCollection parameters = new DataAccessParameterCollection(); 
                string sSql = "SELECT * FROM SYS_LoginLog WHERE 1=1 ";  
                if (!String.IsNullOrEmpty(loginUser))
                {
                    sSql += " AND (Lower(" + SysLoginlogTable.C_USERID + ") like @oper";
                    parameters.AddWithValue("@oper", "%" + loginUser.ToLower() + "%");
                    sSql += " OR Lower(" + SysLoginlogTable.C_USERNAME + ") like @opername )";
                    parameters.AddWithValue("@opername", "%" + loginUser.ToLower() + "%");
                }
                if (!String.IsNullOrEmpty(startDate))
                {
                    sSql += " AND " + SysLoginlogTable.C_LOGINTIME + " >=@StartDate";
                    parameters.AddWithValue("@StartDate", Convert.ToDateTime(startDate));
                }
                if (!String.IsNullOrEmpty(endDate))
                {
                    sSql += " AND " + SysLoginlogTable.C_LOGINTIME + " <=@EndDate";
                    parameters.AddWithValue("@EndDate", Convert.ToDateTime(endDate + " 23:59:59"));
                }
                if (!String.IsNullOrEmpty(ip))
                {
                    sSql += " AND " + SysLoginlogTable.C_IPADDRESS + " LIKE @IP";
                    parameters.AddWithValue("@IP", "%" + ip + "%");
                }
                if (!String.IsNullOrEmpty(loginResult))
                {
                    sSql += " AND " + SysLoginlogTable.C_LOGINRESULT + " =@Result";
                    parameters.AddWithValue("@Result", loginResult);
                }
                sSql += " ORDER BY " + SysLoginlogTable.C_LOGINTIME + " DESC ";
                count = 0;
                return DataAccessCommon.ExecuteDataTablePaging(sSql, pageIndex, pageSize, parameters, out count);
                #endregion
        }
    }
}
